﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Cronos.Silverlight.Validation;
using System.Windows.Data;

public static class ValidationExtensions
{
    public static void SetValidationMessage(this FrameworkElement frameworkElement, string message)
    {
        CustomValidation customValidation = new CustomValidation(message);
        Binding binding = new Binding("ValidationError")
        {
            Mode = System.Windows.Data.BindingMode.TwoWay,
            NotifyOnValidationError = true,
            ValidatesOnExceptions = true,
            Source = customValidation
        };

        frameworkElement.SetBinding(Control.TagProperty, binding);
        frameworkElement.RaiseValidationError();
    }

    private static void RaiseValidationError(this FrameworkElement frameworkElement)
    {
        BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);
        if (b != null)
        {
            ((CustomValidation)b.DataItem).ShowErrorMessage = true;
            b.UpdateSource();
        }
    }

    public static void ClearValidationError(this FrameworkElement frameworkElement)
    {
        BindingExpression b = frameworkElement.GetBindingExpression(Control.TagProperty);
        if (b != null)
        {
            ((CustomValidation)b.DataItem).ShowErrorMessage = false;
            b.UpdateSource();
        }
    }
}
